package com.xci.edu.module.foreground.pc.train.controller;

import com.github.lvyanyang.core.BoolResult;
import com.xci.edu.core.entity.EduQuestion;
import com.xci.edu.core.service.EduQuestionFavoriteService;
import com.xci.edu.core.service.EduQuestionService;
import com.xci.edu.core.service.TrainStudentService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/train/favoriteQuestion")
public class FavoriteQuestionController {
    /** 试题库服务 */
    @Resource
    private EduQuestionService eduQuestionService;
    /** 学员培训登录 */
    @Resource private TrainStudentService trainStudentService;
    /** 题库收藏服务 */
    @Resource private EduQuestionFavoriteService eduQuestionFavoriteService;

    /** 试题集*/
    private List<EduQuestion> questionList;
    /** 学员Id*/
    private Long studentId;
    /** 试题*/
    private EduQuestion entity;

    /** 收藏题库页面 */
    @GetMapping("/index")
    public String index() {
        studentId = trainStudentService.getCurrentStudent().getId();
        questionList = eduQuestionService.getQuestionsByStudentIdAndFavoriteType(studentId,2);
        return "foreground/pc/train/eduFavoriteQuestion/index";
    }

    /** 获取试题 */
    @PostMapping("/getQuestion")
    public String getQuestion(@RequestParam(defaultValue = "0" ) Integer index, ModelMap map){
        if (index >= 0 && index < questionList.size()){
            entity = questionList.get(index);
        }
        map.put("entity", entity);
        map.put("index", index);
        map.put("size", questionList.size());
        return "foreground/pc/train/eduFavoriteQuestion/favoriteQuestion";
    }

    /** 收藏题库 */
    @ResponseBody
    @PostMapping("/favoriteQuestion")
    public BoolResult favoriteQuestion(Long questionId, Integer favoriteType, Boolean isFavorite){
        return isFavorite ?
                eduQuestionFavoriteService.insert(studentId, questionId, favoriteType) :
                eduQuestionFavoriteService.delete(studentId, questionId, favoriteType);
    }

    /** 删除 */
    @ResponseBody
    @PostMapping("/deleteQuestion")
    public BoolResult deleteQuestion(Long questionId){
        questionList.removeIf(q->q.getId().equals(questionId));
        return  eduQuestionFavoriteService.delete(studentId, questionId, 2);
    }

    /** 清空 */
    @ResponseBody
    @PostMapping("/clearQuestion")
    public BoolResult clearQuestion(){
        return eduQuestionFavoriteService.clear(studentId,  2);
    }
}
